﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_F5TurnOver : Form
    {
        public frm_F5TurnOver()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);            
            LoadData();

            TurnOver.DoubleClick += TurnOver_DoubleClick;
        }

        void TurnOver_DoubleClick(object sender, EventArgs e)
        {
            callEditForm();
        }
        
        /// <summary>
        /// Load Data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadData()
        {
            TurnOver.Columns.Clear();
            gridTurnOver.DataSource = null;

            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT * FROM [231_TurnOver] ORDER BY [MonthOfYear]";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {                
                gridTurnOver.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("Month", typeof(string));
                newTable.Columns.Add("TurnOver", typeof(float));
                gridTurnOver.DataSource = newTable;                
            }
            TurnOver.Columns[0].Caption = "Month";
            TurnOver.Columns[1].Caption = "Turn Over";            
            TurnOver.BestFitColumns();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        ////////////////////////////////
        /// Call Edit Form
        ////////////////////////////////
        private void callEditForm()
        {
            DataRow row = TurnOver.GetFocusedDataRow();
            frm_Edit_F5TurnOver editForm = new frm_Edit_F5TurnOver();
            editForm.month_id = row[0].ToString();
            if (editForm.LoadData())
            {
                editForm.ShowDialog();
                LoadData();
            }
            else
            {
                MessageBox.Show("Cannot edit record.");
                editForm.Dispose();
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_F5TurnOver addForm = new frm_Add_F5TurnOver();
            addForm.ShowDialog();
            LoadData();
        }

        private void deleteRecord()
        {
            int[] rows = TurnOver.GetSelectedRows();
            if (rows.Length <= 0)
            {
                MessageBox.Show("There is no selected record to delete");
            }
            else
            {
                if (MessageBox.Show("Are you really to delete selected records now?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string[] id_list = new string[rows.Length];
                    for (int i = 0; i < rows.Length; i++)
                    {
                        DataRow selected_row = TurnOver.GetDataRow(rows[i]);
                        id_list[i] = "'" + selected_row[0].ToString() + "'";
                    }
                    string id = string.Join(",", id_list);

                    try
                    {
                        myConnection.Open();
                        string sql = "DELETE FROM [231_TurnOver]"
                            + " WHERE MonthOfYear IN (" + id + ")";
                        SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                        cmd_sql.ExecuteNonQuery();
                        cmd_sql.Dispose();
                        myConnection.Close();
                        MessageBox.Show("Turn Over deleted success.");
                        LoadData();
                    }
                    catch //(Exception ex)
                    {
                        if (myConnection.State == ConnectionState.Open)
                        {
                            myConnection.Close();
                        }
                        //MessageBox.Show(ex.Message);
                        MessageBox.Show("Cannot delete Turn Over. Please try again.");
                    }
                }
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            deleteRecord();
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            callEditForm();
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            Dispose();
        }
    }
}
